<?php
	session_start();
	if(isset($_REQUEST['magh']))
	{
		$magh=$_REQUEST['magh'];
		require("../DAO/DataHelper.php");
		require("../DAO/SanPhamDTO.php");
		require("../DAO/SanPhamDAO.php");
		
		

		$maxItems=SanPhamDAO::DemSoLuongSanPhamTheoMaGianHang($magh);
		
		$rowPerPage=4;
		$maxPages=ceil($maxItems/$rowPerPage);
		$curPage=1;
		if(isset($_REQUEST['curPage']))
		{
			$curPage=$_REQUEST['curPage'];
		}
		$start=($curPage-1)*$rowPerPage;
		$end=$start+$rowPerPage;
		
		
		$result=SanPhamDAO::LaySanPhamTheoMaGianHang($magh,$start,$end);
		if($result!=null)
		{
			$j=$start;
			foreach($result as $s)
			{
				echo("
				<div class='image-col'>
					<img width='120px' height='130px' src='../images/$s->sp_hinh_anh' />
				</div>
				<div class='details-col'>
					<p class='name'><a href='chitietsanpham.php?masp=$s->sp_ma'>$s->sp_ten</a></p>
					<p class='price'>
					  <label>Giá : </label>
					  <span>$s->sp_don_gia VND</span><p>
					<p class='shop'>
					  <label>Shop : </label>
					  <span><a href='#'><b>$s->sp_ten_gian_hang</b></a></span></p>
					<p class='shop-owner'>
					  <label>Chủ shop : </label>
					  <span><a href='#'>$s->sp_chu_gian_hang</a></span></p>
					<p class='quantity'>
					  <label >Số lượng còn : </label>
					  <span><label> $s->sp_so_luong </label><label id='label$j'>$s->sp_don_vi_tinh</label></span></p>
					<p class='details'>
					  <label>Mô tả : <br/>
					  </label><span>
						$s->sp_mo_ta
						</span>
					</p>
					<p class='tag'>
					  <label>Từ khóa : </label>
					  <span><a href='#'>laptop</a> <a href='#'>dell</a> <a href='#'>electronic</a> <a href='#'>compter</a> <a href='#'>digital</a></span></p>
					<label>Số lượng cần mua : </label>
					<input type='text' id='txtSoLuong$j' class='tb quantity' />
					<input name='button' type='button' class='buy btn' onClick='themVaoGioHang($j,$s->sp_ma,$s->sp_don_gia,$s->sp_so_luong);' value='Thêm vào giỏ hàng' />
				</div>");
						
				//$like=0;
				$strlike="<div><p class='like'>";
				if(isset($_SESSION['UserName'])&& isset($_SESSION['IsLogin']))
				{
					$username=$_SESSION['UserName'];
					//$like=SuKienDAO::KiemTraThichSuKien($s->sk_ma,$username);
					if($_SESSION['IsLogin']==true)
					{
						//if($like>0)
						//{
						//	$sl=$s->sk_so_luong_thich-1;
							$strlike=$strlike."<label class='like'  onClick='commentspClick($j);'>Đăng bình luận</label>";
						//}
						//else
						//{
						//	$sl=0;
		
						//	$sl=$s->sk_so_luong_thich;
		
						//	$strlike=$strlike."<label class='like' onClick='likeClick($like,$curPage,$magh,$s->sk_ma);'>Thích ?</label>";
						//	if($sl>0)
							//	$strlike=$strlike."<label class='like' onClick='listLikeClick();'>Có $sl người thích </label>";
							//$strlike=$strlike."&nbsp;|&nbsp<label class='like'  onClick='commentClick($s->sk_ma);'>Đăng bình luận</label>";
						//}
					}
					else
					{
						$strlike=$strlike."<label style='color:red;font-weight:bold' >Đăng nhập để bình luận</label>";
					}
					
				}
				else
				{
					$strlike=$strlike."<label style='color:red;font-weight:bold' >Đăng nhập để bình luận</label>";
				}
				
				
				
				/*****************************/
					$binhluan=SanPhamDAO::DemSoLuongBinhLuan($s->sp_ma);
					if($binhluan>0)
						$strlike=$strlike."&nbsp;|&nbsp<label><a class='like' href='chitietsanpham.php?masp=$s->sp_ma'>Xem tất cả bình luận cho sản phẩm</a></label>";
						$strlike=$strlike."</p>";
					echo("$strlike
					<div class='comment' align='center' id='commentsp$j'>
						<textarea id='sanpham$j'></textarea><br/>
						<input type='button' class='buy btn' value='Bình luận' onClick='submitBinhLuansp($j,$s->sp_ma);'/>
						<input type='button' class='buy btn' value='Hủy' onClick='cancelBinhLuansp($j);'/>
					</div>
				</div>
				
				");
				$j=$j+1;
			}
			
			if($maxItems>0)
			{
				$str="<br/><div class='paper' style='float:left'><table><tr><td>Trang :";
				for($i=1;$i<=$maxPages;$i++)
				{
					if($i==$curPage)
					{
						$str=$str."<b id='curPage' title='$i' style='color:red'>$i&nbsp;&nbsp;</b>";
					}
					else
					{
						$str=$str."<label onclick='indexspClick($i,$magh);' style='text-decoration:none;cursor:pointer;'>$i&nbsp;&nbsp;</label>";
					}
				}
				$str=$str."</tr></table></div>";
				$str=$str."<input type='hidden' value='$magh' id='magh' />";
				echo($str);
			}
		}
		else
		{
			echo("<h4>Chưa có sản phẩm nào hết</h4>");
		}
	}
?>